from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
from django.contrib.auth.hashers import make_password,check_password
from .. import models
from django.core.urlresolvers import reverse
from web.settings import BASE_DIR
import os
from . viewsIndex import uploads



# 后台用户 列表
def userindex(request):
    #获取所用的用户数据
    data=models.Users.objects.filter().exclude(status=3)

    # 获取搜索条件,和参数
    types = request.GET.get('types')
    keywords = request.GET.get('keywords')

    # 判断是否有搜索条件
    if types:
        if types == 'all':
            from django.db.models import Q
            data = data.filter(
                Q(username__contains=keywords) | Q(phone__contains=keywords) | Q(email__contains=keywords) | Q(
                age__contains=keywords))
        elif type == 'status':
            # keywords==禁用
            arr = {'正常': 0, '禁用': 1, '删除': 3}
            data = data.filter(status=arr[keywords])
        else:
            search = {types + '__contains': keywords}
            data = data.filter(**search)

    # 数据分页
    from django.core.paginator import Paginator
    # 实例化分页类
    paginator = Paginator(data, 20)

    # 获取当前页码数  ?p=n
    p=request.GET.get('p',1)
    # 获取当前页的数据
    userlist=paginator.page(p)


    #分配数据
    context={'userlist':userlist}
    return render(request,'myadmin/users/index.html',context)

# 后台会员管理表单
def useradd(request):
    if request.method=='GET':
        return render(request,'myadmin/users/add.html')
    elif request.method=='POST':
        try:
            data=request.POST.dict()
            data.pop('csrfmiddlewaretoken')

            data['password']=make_password(data['password'],None,'pbkdf2_sha256')

            myfile=request.FILES.get('pic_url')
            if myfile:
                data['pic_url']=uploads(myfile)
            else:
                data['pic_url']='/static/pics/user.jpg'

            ob=models.Users(**data)
            ob.save()
            return HttpResponse('<script>alert("添加成功");location.href="'+reverse('myadmin_user_index')+'"</script>')
        except:
            return HttpResponse('<script>alert("添加失败");location.href="'+reverse('myadmin_user_add')+'"</script>')




# 显示编辑表单,执行编辑
def useredit(request,uid):
    ob=models.Users.objects.get(id=uid)
    if request.method=='GET':
        return render(request,'myadmin/users/edit.html',{'uinfo':ob})
    elif request.method=='POST':
        ob.username=request.POST.get('username')
        if request.POST.get('password'):
            ob.password=make_password(request.POST['password'],None,'pbkdf2_sha256')
        ob.phone=request.POST.get('phone')
        ob.email=request.POST.get('email')
        ob.age=request.POST.get('age')
        ob.sex=request.POST.get('sex')
        myfile=request.FILES.get('pic_url')
        if myfile:
            if ob.pic_url !='/static/pics/user.jpg':
                os.remove(BASE_DIR+ob.pic_url)
            ob.pic_url=uploads(myfile)
        ob.save()
        return HttpResponse('<script>alert("更新成功");location.href="'+reverse('myadmin_user_index')+'"</script>')

# 会员删除  逻辑删除
def userdel(request,uid):
    try:
        ob=models.Users.objects.get(id=uid)
        ob.status=3
        ob.save()
        return HttpResponse('<script>alert("删除成功");location.href="'+reverse('myadmin_user_index')+'"</script>')
    except:
        return HttpResponse('<script>alert("删除失败");location.href="'+reverse('myadmin_user_index')+'"</script>')



